package com.longrise.android.byjk.plugins.aboutme.personalInfo.change;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.PersonalPresenter;
import com.longrise.android.byjk.plugins.request.AppBaseRequest;
import com.longrise.android.byjk.plugins.request.AppBaseResult;
import com.longrise.android.byjk.plugins.request.ChangeInforRequest;
import com.longrise.android.byjk.plugins.request.RequestThread;
import com.longrise.android.byjk.utils.CheckCard;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.NetUtil;
import com.longrise.android.byjk.utils.PhoneUtils;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.android.byjk.widget.view.BaseFrameView;
import com.longrise.android.byjk.widget.view.FalterTextWatcher;


/**
 * Created by callxkj on 2016/11/21.
 * 更改昵称 个人简介 执业信息等
 */

public class ChangeInfoActivity extends BaseActivity implements View.OnClickListener, BaseFrameView.ReloadListener {

    private static final String TAG = "ChangeInfoActivity";

    private final String CHANGE_INFOR_API = "bbt_user_uPersonInfo"; //修改个人资料api 勿动

    private final String SAVEINSTANCE_TYPE = "ChangeInfoActivity_TYPE";
    private final String SAVEINSTANCE_IS_CHANGE_OPTION = "SAVE_IS_CHANGE_OPTION";
    private final String SAVEINSTANCE_IS_INTEGRA = "SAVEINSTANCE_IS_INTEGRA";

    private Toolbar mTitle;

    private RelativeLayout mRelaRight;

    private EditText mEt1;
    private EditText mEt2;
    private TextView mTextXZ;
    private LinearLayout mLlzyxx;

    private int mType;

    private boolean mCurrentOption = false; //确认当前是否为保存操作
    private boolean mIsChange = false; //确认当前是否修改过

    /**
     * 执业信息
     */
    private EditText mEditZYZ;
    private EditText mEditZZZS;
    private EditText mEditSSGS;
    private EditText mEditYWFW;
    private EditText mEditZYQY;
    private EditText mEditCYNX;

    private boolean mIntegralFrom;  //是否是积分过来

    private View mLoading;

    private TextView textRemark;

    private TextView textProfileRemark;

    private TextView save;

    @Override
    public int getContentViewId(@Nullable Bundle state) {
        if (state != null) {
            onRestoreState(state);
        } else {
            getExtraData();
        }
        return R.layout.activity_changeinfo;
    }

    private void getExtraData() {
        Intent extraIntent = getIntent();
        mType = extraIntent.getIntExtra("type", 0);
//        String value = extraIntent.getStringExtra(BBWebHelper.FROM_PAGE);
//        if (value != null && value.equals(BBWebHelper.PAGE_NAME)) {
//            mIntegralFrom = true;
//        } else {
//            mIntegralFrom = false;
//        }
    }

    private void onRestoreState(Bundle state) {
        if (state != null) {
            mType = state.getInt(SAVEINSTANCE_TYPE);
            mIntegralFrom = state.getBoolean(SAVEINSTANCE_IS_INTEGRA);
            mIsChange = state.getBoolean(SAVEINSTANCE_IS_CHANGE_OPTION);
        }
    }

    public void initView() {
        mEt1 = (EditText) findViewById(R.id.changeinfo_et);
        mEt2 = (EditText) findViewById(R.id.changeinfo_et2);
        mTextXZ = (TextView) findViewById(R.id.text_profile_xz);
        mLlzyxx = (LinearLayout) findViewById(R.id.changeinfo_zyxx_ll);
        mLoading = findViewById(R.id.rela_load_xqpc);
        showLoading(false);
        initTitle();
        regEvent();
        initData();

        switchQuery();
    }

    private void initTitle() {
        mTitle = (Toolbar) findViewById(R.id.byjk_toolbar);
        mTitle.setTitle("");
        setSupportActionBar(mTitle);
        mTitle.setNavigationOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                close();
            }
        });

        mRelaRight = (RelativeLayout) mTitle.findViewById(R.id.bb_toolbar_right_rl_tv);
        mRelaRight.setVisibility(View.VISIBLE);
        mRelaRight.setOnClickListener(this);
        save = (TextView) mTitle.findViewById(R.id.iv_toolbar_right_tv);
        save.setText(getString(R.string.savepic));
    }

    /**
     * init执业信息
     * mType == PersonalPresenter.CHANGEZYXX 时初始化
     */
    private void initZYXX() {
        mEditZYZ = (EditText) findViewById(R.id.changeinfo_et_zyz);
        mEditZZZS = (EditText) findViewById(R.id.changeinfo_et_zzzs);
        mEditSSGS = (EditText) findViewById(R.id.changeinfo_et_shgs);
        mEditYWFW = (EditText) findViewById(R.id.changeinfo_et_ywfw);
        mEditZYQY = (EditText) findViewById(R.id.changeinfo_et_zyqy);
        mEditCYNX = (EditText) findViewById(R.id.changeinfo_et_cynx);
    }

    private void regEvent() {

        mEt1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {

                String mEt1Length = mEt1.getText().toString();
                if(mType == PersonalPresenter.CHANGENICKNAME ){
                    if(TextUtils.isEmpty(mEt1Length)){
                        mRelaRight.setEnabled(false);
                        save.setTextColor(Color.parseColor("#FFB3B3B3"));
                    }else {
                        mRelaRight.setEnabled(true);
                        save.setTextColor(Color.parseColor("#FF000000"));
                    }
                }
            }
        });

        mEt2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String profile = mEt2.getText().toString();
                int i = 120 - profile.length();
                setProfileRemark(i);
                if (mType == PersonalPresenter.CHANGEPROFILE){
                    if( TextUtils.isEmpty(profile)){
                        mRelaRight.setEnabled(false);
                        save.setTextColor(Color.parseColor("#FFB3B3B3"));
                    }else {
                        mRelaRight.setEnabled(true);
                        save.setTextColor(Color.parseColor("#FF000000"));
                    }
                }
            }
        });
    }

    private void initData() {
        switch (mType) {
            case PersonalPresenter.CHANGENICKNAME: //昵称
                setEdit1or2Status(View.VISIBLE, View.GONE, getString(R.string.change_nick));
                mEt1.setHint(getString(R.string.save_nikename_hint));
                mLlzyxx.setVisibility(View.GONE);
                mTextXZ.setVisibility(View.GONE);
                setRemarkInfo(getString(R.string.nickinput_type));
                addGoogleScreen("昵称");
                break;

            case PersonalPresenter.CHANGETRUENAME: //真实姓名
                setEdit1or2Status(View.VISIBLE, View.GONE, getString(R.string.change_zsxm));
                mEt1.setHint(getString(R.string.save_truename_hint));
                setRemarkInfo(getString(R.string.truename_remark));
                textRemark.setTextColor(Color.parseColor("#FBA140"));
                mLlzyxx.setVisibility(View.GONE);
                mTextXZ.setVisibility(View.GONE);
                addGoogleScreen("真实姓名");
                break;

            case PersonalPresenter.CHANGEIDNUMBER: //身份证号
                setEdit1or2Status(View.VISIBLE, View.GONE, getString(R.string.change_sfzh));
                mEt1.setHint(getString(R.string.save_sfzh_hint));
                setRemarkInfo(getString(R.string.sfzh_remark));
                textRemark.setTextColor(Color.parseColor("#FBA140"));
                mLlzyxx.setVisibility(View.GONE);
                mTextXZ.setVisibility(View.GONE);
                addGoogleScreen("身份证号");
                break;

            case PersonalPresenter.CHANGEPHONENUMBER: //手机号码
                setEdit1or2Status(View.VISIBLE, View.GONE, getString(R.string.change_phone));
                mEt1.setHint(getString(R.string.save_phonenum_hint));
                setRemarkInfo("");
                mLlzyxx.setVisibility(View.GONE);
                mTextXZ.setVisibility(View.GONE);
                break;

            case PersonalPresenter.CHANGEPROFILE: //个人简介
                setEdit1or2Status(View.GONE, View.VISIBLE, getString(R.string.change_grjj));
                mLlzyxx.setVisibility(View.GONE);
                mEt2.setHint(getString(R.string.save_profile_remark));
                mTextXZ.setVisibility(View.GONE);
//                setProfileRemark(getString(R.string.text_xz) + (120 - profile.length()) + getString(R.string.text_char));

                addGoogleScreen("个人简介");
                break;

            case PersonalPresenter.CHANGEZYXX: //执业信息
                setEdit1or2Status(View.GONE, View.GONE, getString(R.string.change_zyxx));
                initZYXX();
                mLlzyxx.setVisibility(View.VISIBLE);
                mTextXZ.setVisibility(View.GONE);

                if(mRelaRight != null){
                    mRelaRight.setVisibility(View.GONE);  //V1.3.7 执业信息已不允许保存
                }
                setZYXXBtn(false); //保存功能已取消，故直接为false
                addGoogleScreen("执业信息");
                break;

            default:
                break;
        }
    }

    private void setZYXHint() {
        mEditZYZ.setHint(getString(R.string.zyzhint));
        mEditZZZS.setHint(getString(R.string.zzzhint));
        mEditSSGS.setHint(getString(R.string.ssgshint));
        mEditYWFW.setHint(getString(R.string.ywfwhint));
        mEditZYQY.setHint(getString(R.string.zyqyhint));
        mEditCYNX.setHint(getString(R.string.cynxhint));
    }

    private void setEdit1or2Status(int edit1Status, int edit2Status, String title) {
        mTitle.setTitle(title);
        mEt1.setVisibility(edit1Status);
        mEt2.setVisibility(edit2Status);
    }

    private void setRemarkInfo(String remark) {
        textRemark= (TextView) findViewById(R.id.textview_save_bz);
        textRemark.setVisibility(View.VISIBLE);
        textRemark.setText(remark);
    }

    //设置个人简介的remark
    private void setProfileRemark(int i){
        textProfileRemark= (TextView) findViewById(R.id.textview_profile_bz);
        textProfileRemark.setVisibility(View.VISIBLE);
        if(i >=100){
            textProfileRemark.setText(getString(R.string.text_xz) + i + getString(R.string.text_char));
            textProfileRemark.setTextColor(Color.parseColor("#B3B3B3"));
        }else if(i < 100 && i >= 10){
            textProfileRemark.setText(getString(R.string.text_xz) + "0" + i + getString(R.string.text_char));
            textProfileRemark.setTextColor(Color.parseColor("#B3B3B3"));
        }else if(i < 10){
            textProfileRemark.setTextColor(Color.parseColor("#FBA140"));
            textProfileRemark.setText(getString(R.string.text_sign) + "00" + i + getString(R.string.text_char));
        }

    }

    private boolean isEmpty(String params) {
        if (TextUtils.isEmpty(params)) {
            return false;
        }
        return true;
    }

    private boolean isEmpty_Toast(String params) {
        if (TextUtils.isEmpty(params)) {
            switch (mType) {
                case PersonalPresenter.CHANGENICKNAME: //昵称
                    mRelaRight.setEnabled(false);
                    save.setTextColor(Color.parseColor("#FFB3B3B3"));
                    showToast(getString(R.string.nikename_toast));
                    break;

                case PersonalPresenter.CHANGETRUENAME: //真实姓名
                    showToast(getString(R.string.truename_toast));
                    break;

                case PersonalPresenter.CHANGEIDNUMBER: //身份证号
                    showToast(getString(R.string.sfzhname_toast));
                    break;

                case PersonalPresenter.CHANGEPHONENUMBER: //手机号码
                    showToast(getString(R.string.phonenum_toast));
                    break;

                case PersonalPresenter.CHANGEPROFILE: //个人简介
                    mRelaRight.setEnabled(false);
                    save.setTextColor(Color.parseColor("#FFB3B3B3"));
                    showToast(getString(R.string.profile_toast));
                    break;

                case PersonalPresenter.CHANGEZYXX: //执业信息
                    break;

                default:
                    break;
            }
            return false;
        }
        return true;
    }

    private void switchQuery() {
        switch (mType) {
            case PersonalPresenter.CHANGENICKNAME: //昵称
                String nickName = UserInfor.getInstance().getUserNickName();
                mEt1.setText(nickName);
                if (!TextUtils.isEmpty(nickName)) {
                    mEt1.setSelection(nickName.length());
                }
                softKey(mEt1);
                break;

            case PersonalPresenter.CHANGETRUENAME: //真实姓名
                String trueName = UserInfor.getInstance().getPersonname();
                mEt1.setText(trueName);
                if (!TextUtils.isEmpty(trueName)) {
                    mEt1.setSelection(trueName.length());
                } else {
                    softKey(mEt1);
                    mEt1.addTextChangedListener(new FalterTextWatcher(mEt1));
                    mEt1.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
                }
                setWeightOption(TextUtils.isEmpty(trueName));
                break;

            case PersonalPresenter.CHANGEIDNUMBER: //身份证号
                String isRealCardNo = UserInfor.getInstance().getIsRealCardNo();
                String usersfzh = UserInfor.getInstance().getUsersfzh();
                mEt1.setText(isRealCardNo.equals("1") ? usersfzh : "");
                if (isRealCardNo.equals("1") && !TextUtils.isEmpty(usersfzh)) {
                    mEt1.setSelection(usersfzh.length());
                } else {
                    softKey(mEt1);
                }
                setWeightOption(!isRealCardNo.equals("1"));
                break;

            case PersonalPresenter.CHANGEPHONENUMBER: //手机号码
                String userphone = UserInfor.getInstance().getUserphone();
                mEt1.setText(userphone);
                mEt1.setInputType(InputType.TYPE_CLASS_PHONE);
                if (!TextUtils.isEmpty(userphone)) {
                    mEt1.setSelection(userphone.length());
                } else {
                    softKey(mEt1);
                }
                setWeightOption(true);
                break;

            case PersonalPresenter.CHANGEPROFILE: //个人简介
                String proflie = UserInfor.getInstance().getUserProfile();
                mEt2.setText(proflie);
                if (!TextUtils.isEmpty(proflie)) {
                    mEt2.setSelection(proflie.length());
                }
                softKey(mEt2);
                break;

            case PersonalPresenter.CHANGEZYXX: //执业信息
                if (checkNetwork()) {
                    showLoading(true);
                    reqeustZHXX();
                }
                break;

            default:
                break;
        }
    }

    private void setWeightOption(boolean isOption) {
        mRelaRight.setVisibility(isOption ? View.VISIBLE : View.GONE);
        mEt1.setEnabled(isOption ? true : false);
    }

    private void switchResult(EntityBean bean, int resultstate) {
        switch (mType) {
            case PersonalPresenter.CHANGENICKNAME: //昵称
                break;

            case PersonalPresenter.CHANGETRUENAME: //真实姓名
                break;

            case PersonalPresenter.CHANGEIDNUMBER: //身份证号
                break;

            case PersonalPresenter.CHANGEPHONENUMBER: //手机号码
                break;

            case PersonalPresenter.CHANGEPROFILE: //个人简介
                break;

            case PersonalPresenter.CHANGEZYXX: //执业信息
                if (resultstate == 1 && bean != null) {
                    int certfrom = bean.getInt("certfrom");
//                    mRelaRight.setVisibility(certfrom == 1 ? View.GONE : View.VISIBLE);
                    String zyz = bean.getString("certtype");
                    mEditZYZ.setText(zyz);           //执业证
                    mEditZZZS.setText(bean.getString("qualificationtype")); //资质证书
                    mEditSSGS.setText(bean.getString("orgname"));           //所属公司
                    mEditYWFW.setText(bean.getString("scopecodename"));     //业务范围
                    mEditZYQY.setText(bean.getString("scopeareaname"));     //执业区域
                    mEditCYNX.setText(bean.getString("workingyears"));      //从业年限

                    //V1.3.7修改，查询执业信息，强制将mIsChange==true，并设置certdesc 在个人资料重新设置
                    mIsChange = true;
                    UserInfor.getInstance().setUserZYXX(bean.getString("certdesc"));

                    if (!TextUtils.isEmpty(zyz)) {
                        mEditZYZ.setSelection(zyz.length());
                    }

                    setRemarkInfo(certfrom == 1 ? getString(R.string.certfrom_1) : getString(R.string.certfrom_0));
//                    setZYXXBtn(certfrom == 1 ? false);  // 不在允许保存功能，故直接为false
                } else {
//                    mRelaRight.setVisibility(View.GONE);
                }
                setZYXHint();  //默认hint信息，
                break;

            default:
                break;
        }
    }

    private void setZYXXBtn(boolean isEnable) {
        mEditZYZ.setEnabled(isEnable);
        mEditZZZS.setEnabled(isEnable);
        mEditSSGS.setEnabled(isEnable);
        mEditYWFW.setEnabled(isEnable);
        mEditZYQY.setEnabled(isEnable);
        mEditCYNX.setEnabled(isEnable);
    }

    private void save() {
//        EntityBean bean = new EntityBean();
//        bean.set("userid", UserInfor.getInstance().getUserid());
//        bean.set("precardno", UserInfor.getInstance().getUsersfzh());

        switch (mType) {
            case PersonalPresenter.CHANGENICKNAME: //昵称
                String nc = mEt1.getText().toString().trim();
                if (!nc.matches(getString(R.string.regusername))) {
                    showToast(getString(R.string.nickinput_type));
                    return;
                }
                if (isEmpty_Toast(nc)) {
                    setCurrentOption(true);
                    Bundle bundle = new Bundle();
                    bundle.putString("nickname", nc);
                    commit(CHANGE_INFOR_API, PersonalPresenter.CHANGENICKNAME, bundle);
                }
                Tracker_Google.getInstance().addEvent("Personal-data", "click-nickname", "昵称");
                break;

            case PersonalPresenter.CHANGETRUENAME: //真实姓名
                String zsxm = mEt1.getText().toString().trim();
                if (isEmpty_Toast(zsxm)) {
                    if (!zsxm.matches("[\u4e00-\u9fa5]+")) {
                        DZZWTools.showToast(this, getString(R.string.inputtype_zw), Toast.LENGTH_SHORT);
                        return;
                    }
                    setCurrentOption(true);
                    Bundle bundle = new Bundle();
                    bundle.putString("personname", zsxm);
                    commit(CHANGE_INFOR_API, PersonalPresenter.CHANGETRUENAME, bundle);
                }
                Tracker_Google.getInstance().addEvent("Personal-data", "click-name", "真实姓名");
                break;

            case PersonalPresenter.CHANGEIDNUMBER: //身份证号
                String sfzh = mEt1.getText().toString().trim();
                if (isEmpty_Toast(sfzh)) {
                    try {
                        if (!CheckCard.IDCardValidate(sfzh)) {
                            showToast(getString(R.string.cardno_type_error));
                            return;
                        }
                        setCurrentOption(true);
                        Bundle bundle = new Bundle();
                        bundle.putString("cardno", sfzh);
                        commit(CHANGE_INFOR_API, PersonalPresenter.CHANGEIDNUMBER, bundle);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                Tracker_Google.getInstance().addEvent("Personal-data", "click-ID", "身份证");
                break;

            case PersonalPresenter.CHANGEPHONENUMBER: //手机号码
                String sjhm = mEt1.getText().toString().trim();
                if (isEmpty_Toast(sjhm)) {
                    if (!PhoneUtils.isPhoneNumber(sjhm)) {
                        DZZWTools.showToast(this, getString(R.string.phonenum_type_error), Toast.LENGTH_SHORT);
                        return;
                    }
                    setCurrentOption(true);
                    Bundle bundle = new Bundle();
                    bundle.putString("mobile", sjhm);
                    commit(CHANGE_INFOR_API, PersonalPresenter.CHANGEPHONENUMBER, bundle);
                }
                Tracker_Google.getInstance().addEvent("Personal-data", "click-phone", "手机号码");
                break;

            case PersonalPresenter.CHANGEPROFILE: //个人简介
                String grjj = mEt2.getText().toString().trim();
                if (isEmpty_Toast(grjj)) {
                    setCurrentOption(true);
                    Bundle bundle = new Bundle();
                    bundle.putString("profile", grjj);
                    commit(CHANGE_INFOR_API, PersonalPresenter.CHANGEPROFILE, bundle);
                }
                Tracker_Google.getInstance().addEvent("Personal-data", "click-personal-profile", "个人简介");
                break;

            case PersonalPresenter.CHANGEZYXX: //执业信息
                String zyz = mEditZYZ.getText().toString().trim();    //执业证
                String zzzs = mEditZZZS.getText().toString().trim();  //资质证书
                String ssgs = mEditSSGS.getText().toString().trim();  //所属公司
                String ywfw = mEditYWFW.getText().toString().trim();  //业务范围
                String zyqy = mEditZYQY.getText().toString().trim();  //执业区域
                String cynx = mEditCYNX.getText().toString().trim();  //从业年限
                if (!isEmpty(zyz) && !isEmpty(zzzs) && !isEmpty(ssgs) && !isEmpty(ywfw) && !isEmpty(zyqy) && !isEmpty(cynx)) {
                    showToast(getString(R.string.zyxxtoast));
                    return;
                }
                setCurrentOption(true);
                Bundle bundle = new Bundle();
                bundle.putString("certtype", zyz);
                bundle.putString("qualificationtype", zzzs);
                bundle.putString("orgname", ssgs);
                bundle.putString("scopecodename", ywfw);
                bundle.putString("scopeareaname", zyqy);
                bundle.putString("workingyears", cynx);
                commit("bbt_user_uLocalDevCert", PersonalPresenter.CHANGEZYXX, bundle);
                Tracker_Google.getInstance().addEvent("Personal-data", "click-practice-information", "执业信息");
                break;

            default:
                break;
        }
    }

    private void reqeustZHXX() {
        commit("bbt_user_sDevCertInfo", AppBaseRequest.APP_REQUEST_TAG, null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bb_toolbar_right_rl_tv:
                save();
                break;
        }
    }

    private void setCurrentOption(boolean boo) {
        mCurrentOption = boo;
    }

    private void commit(String api, int tag, Bundle bundle) {
//        mRelaRight.setEnabled(true);
//        save.setTextColor(Color.parseColor("#FF000000"));
        showLoading(true);
        ChangeInforRequest changeInforRequest = new ChangeInforRequest(new ChangeInforResult());
        changeInforRequest.setApi(api);
        RequestThread requestThread = new RequestThread(changeInforRequest);
        requestThread.request(this, tag, bundle);
    }

    private class ChangeInforResult implements AppBaseResult {

        @Override
        public void onSuccess(Object result) {
            if (isFinishing()) {
                return;
            }
            EntityBean bean = (EntityBean) result;
            if (bean == null) {
                //数据结构异常
                return;
            }
            int resultstate = bean.getInt("resultstate");
            if (resultstate != 1) {
                showToast(bean.getString("resultdesc"));
                return;
            }
            if (mCurrentOption) {
                //保存操作
                mIsChange = true;
                EntityBean resutBean = bean.getBean("result");
                switchClose(resutBean);//设置本地数据
                if (mType == PersonalPresenter.CHANGEPHONENUMBER) {
                    //绑定手机号时不存在积分情况
                    close();
                } else {
                    parseIntegral(resutBean); //解析积分相关信息
                }
                showToast(bean.getString("resultdesc"));
            } else {
                switchResult(bean.getBean("result"), resultstate);
            }
        }

        @Override
        public void onFailed(String errorLog) {
            showToast(getString(R.string.nonetwork));
        }

        @Override
        public void onFinish() {
            setCurrentOption(false);
            showLoading(false);
        }
    }

    private void parseIntegral(EntityBean integral) {
//        if (!mIntegralFrom) {
//            //正常入口进入
//            close();
//            return;
//        }
        String infostate = integral.getString("infostate");  //任务完成状态 (0:完成失败;1:成功; 2:已经完成过)
        String acquiredscore = integral.getString("acquiredscore"); //新增的积分
        String remindwords = integral.getString("remindwords"); //提示语
        if (infostate != null && infostate.equals("1")) {
//            MyIntegralDialogUtil.showIntegralDailog(this, acquiredscore, remindwords, mIntegralFrom);
        } else if (infostate != null && infostate.equals("0")) {
            showToast(remindwords);
        } else {
            //已完成过
            close();
        }
    }

    @Override
    public void onReload(View v) {
        if (checkNetwork()) {
            switchQuery();
        }
    }

    private boolean checkNetwork() {
        if (!NetUtil.checkNetEnable()) {
            return false;
        } else {
            return true;
        }
    }

    private void updateBtnState() {
//        mRelaRight.setVisibility(View.GONE);
        mEt1.setEnabled(false);
    }

    private void switchClose(EntityBean changeBean) {
        switch (mType) {
            case PersonalPresenter.CHANGENICKNAME: //昵称
                UserInfor.getInstance().setUserNickName(changeBean.getString("nickname"));
                break;

            case PersonalPresenter.CHANGETRUENAME: //真实姓名
                UserInfor.getInstance().setPersonname(changeBean.getString("personname"));
                updateBtnState();  //真实姓名只允许用户输入一次，故完善信息后隐藏保存按钮
                break;

            case PersonalPresenter.CHANGEIDNUMBER: //身份证号
                String cardno = changeBean.getString("cardno");
                UserInfor.getInstance().setUsersfzh(cardno);
                if (!TextUtils.isEmpty(cardno)) {
                    UserInfor.getInstance().setIsRealCardNo("1");
                    UserInfor.getInstance().setCardtype("0");
                }
                updateBtnState();  //身份证号只允许用户输入一次，故完善信息后隐藏保存按钮
                break;

            case PersonalPresenter.CHANGEPHONENUMBER: //手机号码
                UserInfor.getInstance().setUserphone(changeBean.getString("mobile"));
                Tracker_Google.getInstance().addEvent("Phone-binding", "Binding-success-phone", "绑定成功");
                break;

            case PersonalPresenter.CHANGEPROFILE: //个人简介
                UserInfor.getInstance().setUserProfile(changeBean.getString("profile"));
                break;

            case PersonalPresenter.CHANGEZYXX: //执业信息
                UserInfor.getInstance().setUserZYXX(changeBean.getString("certtype"));
                break;

            default:
                break;
        }
    }

    private void softKey(EditText editText) {
        try {
            editText.requestFocus();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void close() {
        setResult(-10, new Intent().putExtra("key", mIsChange));
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void addGoogleScreen(String screenName) {
        Tracker_Google.getInstance().addGoogleScreenName(screenName);
    }

    private void showLoading(boolean isLoading) {
        mLoading.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVEINSTANCE_TYPE, mType);
        outState.putBoolean(SAVEINSTANCE_IS_CHANGE_OPTION, mIsChange);
        outState.putBoolean(SAVEINSTANCE_IS_INTEGRA, mIntegralFrom);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setCurrentOption(false);
    }
}
